import { setStore, getStore, removeStore } from "@/plugins/storage.js";

/** @name token缓存的key **/
const ACCESS_TOKEN_KEY = "access_token";
/** @name 刷新token缓存的key **/
const REFRESH_TOKEN_KEY = "refresh_token";
/** @name 用户信息的key **/
const USER_INFO_KEY = "user_info";
/** @name 记录从打开网站到关闭的生命周期 **/
const WEB_LIFE_CYCLE = "web_life_cycle";

// Token
export function getToken() {
  return getStore({ name: ACCESS_TOKEN_KEY }) || "";
}
export function setToken(token) {
  setStore({
    name: ACCESS_TOKEN_KEY,
    content: token,
  });
}
export function clearToken() {
  removeStore({ name: ACCESS_TOKEN_KEY });
}

// RefreshToken
export function getRefreshToken() {
  return getStore({ name: REFRESH_TOKEN_KEY }) || "";
}
export function setRefreshToken(token) {
  setStore({
    name: REFRESH_TOKEN_KEY,
    content: token,
  });
}
export function clearRefreshToken() {
  removeStore({ name: REFRESH_TOKEN_KEY });
}

// UserInfo
export function getUserInfo() {
  return getStore({ name: USER_INFO_KEY }) || "";
}
export function setUserInfo(userInfo) {
  setStore({
    name: USER_INFO_KEY,
    content: userInfo,
  });
}
export function clearUserInfo() {
  removeStore({ name: USER_INFO_KEY });
}

/** @name 清空登录信息 **/
export function clearLoginInfo() {
  clearToken();
  clearRefreshToken();
  clearUserInfo();
}

// WebLifeCycle
export function getWebLifeCycle() {
  return getStore({ name: WEB_LIFE_CYCLE, type: "session" }) || false;
}
export function setWebLifeCycle(bool) {
  setStore({
    name: WEB_LIFE_CYCLE,
    content: bool,
    type: "session",
  });
}
